package lk.exception;

/**
 * 全局restful异常处理工具
 */
public class ExceptionUtil {

    public static String getStackTrace( Exception e ) {
        StringBuilder sb = new StringBuilder( e.getClass().getName() ).append( e.getMessage() ).append( "\n" );
        for ( StackTraceElement stackTraceElement : e.getStackTrace() ) {
            sb.append( stackTraceElement.getClassName() ).append( "." ).append( stackTraceElement.getMethodName() )
                    .append( "(" ).append( stackTraceElement.getFileName() ).append( ":" ).append( stackTraceElement.getLineNumber() ).append( ")\n" );
        }
        return sb.toString();
    }

    public static String getStackTraceFlatString( StackTraceElement[] stackTraceElements ) {
        StringBuilder sb = new StringBuilder().append( "\n" );
        for ( StackTraceElement stackTraceElement : stackTraceElements ) {
            sb.append( stackTraceElement.getClassName() ).append( "." ).append( stackTraceElement.getMethodName() )
                    .append( "(" ).append( stackTraceElement.getFileName() ).append( ":" ).append( stackTraceElement.getLineNumber() ).append( ")\n" );
        }
        return sb.toString();
    }

}
